package typejump;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

import javax.imageio.ImageIO;

public class ImageManager {
	private static Hashtable<String, BufferedImage> cache = new Hashtable<String, BufferedImage>();
	
	
	public static BufferedImage getImage(String name) {
		if (cache.containsKey(name))
			return cache.get(name);
		try {
			BufferedImage img = ImageIO.read(ImageManager.class.getResource("/typejump/res/" + name));
			if (img != null) {
				cache.put(name, img);
				return img;
			}
		} catch (Exception e) {
			
		}
		return null;
	}
	
	public static BufferedImage rotate90(BufferedImage img) {
		BufferedImage nw = new BufferedImage(img.getHeight(), img.getWidth(), img.getType());
		Graphics2D g = nw.createGraphics();
		g.translate(nw.getWidth(), 0);
		g.rotate(Math.toRadians(90));
		g.drawImage(img, 0, 0, null);
		g.dispose();
		return nw;
	}
	
	public static BufferedImage rotate90(BufferedImage img, int times) {
		if (times <= 0) return img;
		return rotate90(rotate90(img), times-1);
	}
}
